import React from 'react';
import type { ProFormColumnsType } from "@ant-design/pro-components";
import { BetaSchemaForm } from '@ant-design/pro-components';
import type { FormInstance } from 'antd';

/**
 * PC端表单的属性类型
 * @template T - 表单数据类型
 */
export interface PcFormProps<T> {
  /** 表单列配置 */
  columns: ProFormColumnsType<T>[];
  /** 表单提交回调 */
  onFinish: (values: T) => Promise<void>;
  /** 表单重置回调 */
  onReset?: () => void;
  formRef?: React.MutableRefObject<FormInstance | undefined>;
}

/**
 * PC端表单组件
 * @template T - 表单数据类型
 */
const PcForm = <T extends Record<string, any>>(props: PcFormProps<T>) => {
  const { columns, onFinish, onReset, formRef } = props;

  // 处理搜索表单的列配置
  const transformColumns = columns.map(column => {
    if (column.searchTransform) {
      return {
        ...column,
        ...column.searchTransform,
      };
    }
    return column;
  });

  // 包装 onFinish 以返回 boolean
  const handleFinish = async (values: T): Promise<boolean> => {
    try {
      await onFinish(values);
      return true;
    } catch (error) {
      console.error('Form submission failed:', error);
      return false;
    }
  };

  return (
    <BetaSchemaForm<T>
      layoutType="QueryFilter"
      columns={transformColumns}
      formRef={formRef}
      onFinish={handleFinish}
      onReset={onReset}
    />
  );
};

export default PcForm; 